"""获取Orion.Nodes表里所有的字段
根据IP查找节点，获取uri
把关联的表名拼接到uri后面，打印出所有字段的key和value
"""

import requests
from orionsdk import SwisClient

from sds import settings


def main(ip_addr, table_name='/'):
    npm_server = settings.npm_server
    username = settings.username
    password = settings.password

    swis = SwisClient(npm_server, username, password)
    print("先做查询获取uri...")
    results = swis.query(
        "SELECT Uri FROM Orion.Nodes WHERE IP=@ip_addr",
        ip_addr=ip_addr)  # set valid NodeID!
    if len(results['results']) == 0:
        print("未查询到任何节点")
        return
    elif len(results['results']) > 1:
        print("查询到多个节点:", results['results'])
        return
    uri = results['results'][0]['Uri']
    # uri拼接关联的表名，打印出该表里所有的key和value
    # 拼接'/'，就是当前表
    obj = swis.read(uri + table_name)
    for k, v in obj.items():
        print(k, ':', v)


requests.packages.urllib3.disable_warnings()


if __name__ == '__main__':
    main("192.168.130.253", '/')
    main("192.168.130.253", '/CustomProperties')
